package com.suxin.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author Tang
 * @classname Builder
 * @description [ 通用的builder ]
 * @date 2022/5/31 10:14
 */
public class Builder<T> {

    private final Supplier<T> instant;
    private List<Consumer<T>> consumers;

    public Builder(Supplier<T> instant) {
        this.instant = instant;
        consumers = new ArrayList<>();
    }

    public Builder of(Supplier<T> instant) {
        return new Builder(instant);
    }

    public Builder<T> with(Consumer1 consumer1, Object param) {
        Consumer<T> consumer = (instance) -> consumer1.accept(instance, param);
        consumers.add(consumer);
        return this;
    }

    public Builder<T> with(Consumer2 consumer1, Object param, Object param1) {
        Consumer<T> consumer = (instance) -> consumer1.accept(instance, param, param1);
        consumers.add(consumer);
        return this;
    }

    public Builder<T> with(Consumer3 consumer1, Object param, Object param1, Object param2) {
        Consumer<T> consumer = (instance) -> consumer1.accept(instance, param, param1, param2);
        consumers.add(consumer);
        return this;
    }

    public T builder() {
        T t = instant.get();
        for (Consumer consumer : consumers) {
            consumer.accept(t);
        }
        return t;
    }

    @FunctionalInterface
    interface Consumer1<T, K> {
        void accept(T t, K k);
    }

    @FunctionalInterface
    interface Consumer2<T, K, M> {
        void accept(T t, K k, M m);
    }

    @FunctionalInterface
    interface Consumer3<T, K, M, J> {
        void accept(T t, K k, M m, J j);
    }
}

class Dog {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}